package com.xhwl.centiotdata.suppliersystem.lifang;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.LiFangConstant;
import com.xhwl.centiotdata.pojo.dto.lifang.DeviceLiFangDTO;
import com.xhwl.centiotdata.pojo.dto.lifang.VisitorInfoLiFangDTO;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class VisitorLiFangHandler {
    @Autowired
    private GeneralLiFangHandler generalLiFangHandler;
    public ResultJson getQrCode(VisitorIntegrationConfigDTO configDTO, VisitorInfoLiFangDTO visitorInfoLiFangDTO){
        Map<String,Object> params = this.putParam(visitorInfoLiFangDTO);
        JSONObject resultObject = generalLiFangHandler.post(LiFangConstant.VISITOR_QR_CODE, params, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        Integer result = resultObject.getInteger("result");
        if(result != 200){
            log.error("post path:{};errMsg:{}", LiFangConstant.QUERY_VISITOR_DEVICE, resultObject.getString("resultInfo"));
            return ResultJson.fail("操作失败");
        }
        String qccode = resultObject.getString("qrcode");
        return ResultJson.success(qccode);
    }

    public List<DeviceLiFangDTO> pullVisitorDevice(VisitorIntegrationConfigDTO configDTO){
        log.info("-------------开始拉取访客设备信息-------------");
        Map<String, Object> param = new HashMap<>();
        JSONObject resultObject = generalLiFangHandler.post(LiFangConstant.QUERY_VISITOR_DEVICE, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        if (resultObject == null) {
            return null;
        }
        Integer result = resultObject.getInteger("result");
        if(result != 200){
            log.error("post path:{};errMsg:{}", LiFangConstant.QUERY_VISITOR_DEVICE, resultObject.getString("resultInfo"));
            return null;
        }
        String obj = resultObject.getString("obj");
        if(StringUtils.isEmpty(obj)){
            log.error("post path:{};data is null!", LiFangConstant.QUERY_VISITOR_DEVICE);
            return null;
        }
        List<DeviceLiFangDTO> deviceLiFangDTOList = JSONObject.parseArray(obj, DeviceLiFangDTO.class);
        if (CollectionUtils.isEmpty(deviceLiFangDTOList)) {
            return null;
        }
        return deviceLiFangDTOList;
    }
    private Map<String,Object> putParam(VisitorInfoLiFangDTO visitorInfoLiFangDTO){
        Date startDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(startDate);
        String dateStr = startTime.split(" ")[0];
        String endTime = dateStr + " 23:59:59";
        Map<String,Object> params = new HashMap<>();
        params.put("visitorName",visitorInfoLiFangDTO.getVisitorName());
        params.put("sex",visitorInfoLiFangDTO.getSex());
        params.put("visitorCertPath",null);
        params.put("visitorPhotoPath",visitorInfoLiFangDTO.getVisitorPhoto());
        params.put("certType",visitorInfoLiFangDTO.getCertificateType());
        params.put("certNum",visitorInfoLiFangDTO.getCertificateNum());
        params.put("address",visitorInfoLiFangDTO.getVisitorAddress());
        params.put("phone",visitorInfoLiFangDTO.getPhone());
        params.put("carNum",visitorInfoLiFangDTO.getCarNum());
        params.put("company",visitorInfoLiFangDTO.getVisitorCompany());
        params.put("visitReason",1);
        params.put("visitReasonName",visitorInfoLiFangDTO.getVisitPurpose());
        params.put("visitPopulation",1);
        params.put("mark","备注");
        if (StringUtils.isNotEmpty(visitorInfoLiFangDTO.getUserOpenId()))
        {
            params.put("sysNo",Integer.valueOf(visitorInfoLiFangDTO.getUserOpenId()));
        }
        params.put("visitorType",1);
        params.put("visitorTypeName","访客类型");
        if (StringUtils.isNotEmpty(visitorInfoLiFangDTO.getVisitStartTime())){
            params.put("startTime",visitorInfoLiFangDTO.getVisitStartTime());
        }else {
            params.put("startTime",startTime);
        }
        if (StringUtils.isNotEmpty(visitorInfoLiFangDTO.getVisitEndTime())){
            params.put("endTime",visitorInfoLiFangDTO.getVisitEndTime());
        }else {
            params.put("endTime",endTime);
        }
        return params;
    }
}
